function [dimtype, z_type] = rslice_classify_variable ( varinfo )
% RSLICE_CLASSIFY_VARIABLE:  classifies the given variable base on dimensions.
%
% This m-file looks at the named dimensions of the given variable and
% classifies the dimension type as one of "TZYX",  "TYX",  "ZYX", or
% "YX".  The z dimension could be different, depending on whether the
% dimension of record there is either s_rho or s_w.
%
% USAGE:  [dimtype, z_type] = rslice_classify_variable ( varinfo );
%
% PARAMETERS:
% Input:
%     varinfo:  variable metadata structure as returned by nc_getvarinfo
% Output:
%     dimtype:
%         To become one of TZYX, TYX, ZYX, or YX.
%     z_type:  
%         a vslice-able z variable should be either a RHO or W variable.  
%         If the variable does not have a Z dimension, the empty matrix 
%         is used.  Otherwise it should either be 's_rho' or 's_w'.
%
%         If the z dimension is 'Nbed', then 'z_type' will be 
%         'sediment'.  This is a flag so that we can disable
%         vertical slicing here.
%

is_time_variable = 0;
is_z_variable = 0;
is_y_variable = 0;
is_x_variable = 0;

%
% Is it a singleton variable?
if length(varinfo.Size) == 0
    dim_type = '';
    z_type = '';
end

n = length ( varinfo.Dimension );
for j = 1:n
    switch ( varinfo.Dimension{j} )
    case { 'xi_u', 'xi_v', 'xi_rho' }
        is_x_variable = 1;
    case { 'eta_u', 'eta_v', 'eta_rho' }
        is_y_variable = 1;
    case { 's_rho', 's_w', 'Nbed' }
        is_z_variable = 1;
    case {'time', 'ocean_time', 'tide_period' }
        is_time_variable = 1;
    end
end


if is_time_variable
    if is_z_variable
        if is_y_variable
            if is_x_variable
                dimtype = 'TZYX';
            else
                dimtype = 'TZY';
            end
        else
            if is_x_variable
                dimtype = 'TZX';
            else
                dimtype = 'TZ';
            end
        end
    else
        if is_y_variable
            if is_x_variable
                dimtype = 'TYX';
            else
                dimtype = 'TY';
            end
        else
            if is_x_variable
                dimtype = 'TX';
            else
                dimtype = 'T';
            end
        end
    end
else
    if is_z_variable
        if is_y_variable
            if is_x_variable
                dimtype = 'ZYX';
            else
                dimtype = 'ZY';
            end
        else
            if is_x_variable
                dimtype = 'ZX';
            else
                dimtype = 'Z';
            end
        end
    else
        if is_y_variable
            if is_x_variable
                dimtype = 'YX';
            else
                dimtype = 'Y';
            end
        else
            if is_x_variable
                dimtype = 'X';
            else
                dimtype = '';
            end
        end
    end
end

%
% Now classify the z dimension.
z_ind = findstr ( dimtype, 'Z' );
if isempty(z_ind)
    z_type = [];
else
    switch ( varinfo.Dimension{z_ind} )
    case { 's_rho', 's_w' }
        z_type = varinfo.Dimension{z_ind};
    case 'Nbed'
        z_type = 'sediment';
    otherwise
        error( 'RSLICE:classifyVariable', ...
               'unhandled z dimension %s.\n', varinfo.Dimension{z_ind} );
    end
end
